package flst.ipiweb.persistance.dao.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import flst.ipiweb.persistance.dao.IClientDao;
import flst.ipiweb.transverse.Adresse;
import flst.ipiweb.transverse.Client;
import flst.ipiweb.transverse.IPIException;

public class ClientDaoJdbc implements IClientDao{

	private JdbcUtils jdbcUtils = JdbcUtils.getInstance();
	
	@Override
	/**
	 * Cette méthode permet de récupérer un client par son id
	 * @param id
	 * @return le client trouvé
	 */
	public Client getClientById(int id) throws IPIException{
		Client client = new Client();
		
		try {
			Connection vConnection = jdbcUtils.getConnection();
			String vRequeteInsert = "SELECT * FROM IPII_USERS WHERE USER_ID = ?";
			PreparedStatement vPs = vConnection.prepareStatement(vRequeteInsert);
			// LOGIN
			vPs.setInt(1, id);
			
			ResultSet rs = vPs.executeQuery();
			
			
			
			if (rs.next()) {
				client.setIdUser(id);
				client.setLoginUtilisateur(rs.getString("LOGIN"));
				client.setNom(rs.getString("LAST_NAME"));
				client.setPrenom(rs.getString("FIRST_NAME"));
				client.setCiviliteLongue(rs.getString("TITLE"));
				if (rs.getInt("RESPONSIBILITY_ID") == 3) {
					client.setIsSuper(true);
				} else {
					client.setIsSuper(false);
				}
				//ADDRESS_LINE_1 
				Adresse adresse = new Adresse();
				adresse.setAdresse(rs.getString("ADDRESS_LINE_1"));
				//ADDRESS_LINE_2 
				adresse.setComplementsAdresse(rs.getString("ADDRESS_LINE_2"));
				//ZIP_CODE 
				adresse.setCodePostal(rs.getString("ZIP_CODE"));
				//CITY 
				adresse.setVille(rs.getString("CITY"));
				client.setAdresse(adresse);
			} 
			
			vPs.close();
			vConnection.close();
			
			return client;
		} 
		catch (Exception e) {
			throw new IPIException("AdminException.update.technique");
		}
	}
}
